define(['./../module', 'underscore'], function (module, _) {

    module.factory('MainMenuFactory', [function () {
        return {
            Menu: function () {
                return {
                    type: 'categories',
                    categories: {
                        type: 'existing',
                        show: 'both',
                        items: [],
                        render_as: 'megamenu'
                    },
                    products: {
                        source: 'category',
                        module_type: 'featured',
                        items: []
                    },
                    manufacturers: {
                        type: 'all',
                        name: 1,
                        items: []
                    },
                    custom: {
                        top: {
                            menu_type: 'custom',
                            menu_item: null
                        },
                        items: []
                    },
                    icon: { },
                    hide_text: '0',
                    is_open: 1,
                    status: 1,
                    hide_on_mobile: 0,
                    items_per_row: {
                        "range": "1,8",
                        "step": "1",
                        "hide_columns": true,
                        "value": {
                            "mobile": {
                                "value": "1",
                                "range": "1,8",
                                "step": "1"
                            },
                            "mobile1": {
                                "value": "2",
                                "range": "1,8",
                                "step": "1"
                            },
                            "tablet": {
                                "value": "3",
                                "range": "1,8",
                                "step": "1"
                            },
                            "tablet1": {
                                "value": "2",
                                "range": "1,8",
                                "step": "1"
                            },
                            "tablet2": {
                                "value": "1",
                                "range": "1,8",
                                "step": "1"
                            },
                            "desktop": {
                                "value": "4",
                                "range": "1,8",
                                "step": "1"
                            },
                            "desktop1": {
                                "value": "3",
                                "range": "1,8",
                                "step": "1"
                            },
                            "desktop2": {
                                "value": "2",
                                "range": "1,8",
                                "step": "1"
                            },
                            "large_desktop": {
                                "value": "4",
                                "range": "1,8",
                                "step": "1"
                            },
                            "large_desktop1": {
                                "value": "3",
                                "range": "1,8",
                                "step": "1"
                            },
                            "large_desktop2": {
                                "value": "2",
                                "range": "1,8",
                                "step": "1"
                            }
                        }
                    },
                    items_limit: 5,
                    html_blocks: [],
                    title: {}
                };
            },
            HtmlMenuItem: function () {
                return {
                    title: {},
                    status: 1,
                    sort_order: '',
                    text: {},
                    is_open: 1
                };
            },
            MenuItem: function () {
                return {
                    menu: {
                        menu_type: 'custom',
                        menu_item: null
                    },
                    target: 0,
                    is_open: 1
                };
            }
        };
    }]);

    module.controller('MainMenuController', function ($scope, $routeParams, $timeout, Spinner, Rest, MainMenuFactory) {

        $scope.store_id = $routeParams.store_id || Journal2Config.stores[0].store_id;
        $scope.items = [];
        $scope.close_others = false;

        $scope.isLoading = true;
        $timeout(function () {
            Rest.getSetting('mega_menu', $scope.store_id).then(function (response) {
                if (response) {
                    $scope.items = response.items || [];
                    $scope.close_others = response.close_others;
                }
                $scope.items = _.map($scope.items, function (item) {
                    return _.extend(new MainMenuFactory.Menu(), item);
                });
                $timeout(function () {
                    $scope.isLoading = false;
                    Spinner.hide();
                }, 1);

            }, function (error) {
                alert(error);
            });
        }, 500);

        $scope.addMenu = function () {
            $scope.items.push(new MainMenuFactory.Menu());
        };

        $scope.removeMenu = function ($index) {
            $scope.items.splice($index, 1);
        };

        $scope.addSubMenu = function (menu) {
            menu.items.push(new MainMenuFactory.MenuItem());
        };

        $scope.removeSubMenu = function (menu, $index) {
            menu.items.splice($index, 1);
        };

        $scope.addItem = function (menu) {
            menu.items.push({
                menu_type: 'opencart',
                menu_item: {}
            });
        };

        $scope.removeItem = function (menu, $index) {
            menu.items.splice($index, 1);
        };

        $scope.addHtmlBlock = function (menu) {
            menu.html_blocks.push(new MainMenuFactory.HtmlMenuItem());
        };

        $scope.removeHtmlBlock = function (menu, $index) {
            menu.html_blocks.splice($index, 1);
        };

        $scope.save = function ($event) {
            var $src = $($event.srcElement);
            Spinner.show($src);
            Rest.setSetting('mega_menu', $scope.store_id, { items: $scope.items, close_others: $scope.close_others }).then(function (response) {
                Spinner.hide($src);
            }, function (error) {
                Spinner.hide($src);
                alert(error);
            });
        };

        $scope.reset = function ($event) {
            var $src = $($event.srcElement);
            Spinner.show($src);
            Rest.getTopCategories().then(function (response) {
                Spinner.hide($src);
                $scope.items = [];
                _.each(response, function (category) {
                    var item = _.extend(new MainMenuFactory.Menu(), {
                        type: "categories",
                        categories: {
                            type: "existing",
                            show: "both",
                            items: [ ],
                            render_as: "dropdown",
                            top: {
                                id: category.category_id,
                                name: category.name
                            },
                            subcategories: []
                        }
                    });
                    $scope.items.push(item);
                });
            }, function (error) {
                Spinner.hide($src);
                alert(error);
            });
        };

        $scope.toggleAccordion = function (items, model, value) {
            _.each(items, function (item) {
                item.is_open = value;
            });
            if (value) {
                if (model === null) {
                    $scope.close_others = false;
                } else {
                    model.close_others = false;
                }
            }
        };
    });

});